//
//  HJStatusCell.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/9/27.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit
import SDWebImage

let commonMargin : CGFloat = 8
//图片之间的间距
let pictureCellMargin : CGFloat = 3
//图片的宽度
let maxWidth = KScreenW - 2 * commonMargin
//每张图片的宽度
let itemWidth = (maxWidth - 2 * pictureCellMargin)/3

class HJStatusCell: UITableViewCell {

    //用户头像
    @IBOutlet weak var iconView: UIImageView!
    //用户昵称
    @IBOutlet weak var nameLable: UILabel!
    //用户等级
    @IBOutlet weak var memberImage: UIImageView!
    //创建时间
    @IBOutlet weak var timeLable: UILabel!
    //微博来源
    @IBOutlet weak var sourceLable: UILabel!
    //微博正文
    @IBOutlet weak var contentLable: UILabel!
    //微博认证类型
    @IBOutlet weak var avaterImage: UIImageView!
    //配图视图
    @IBOutlet weak var pictureView: HJPictureView!
    
    //配图视图的宽度约束对象
    @IBOutlet weak var pictureViewWidthCons: NSLayoutConstraint!
    //配图视图的高度约束对象
    @IBOutlet weak var pictureViewHeightCons: NSLayoutConstraint!
     //配图视图的顶部间距
    @IBOutlet weak var pictureViewTopCons: NSLayoutConstraint!
    //配图视图的布局对象
    @IBOutlet weak var pictureViewLayout: UICollectionViewFlowLayout!
    
    //评论
    @IBOutlet weak var commentBtn: UIButton!
    //转发
    @IBOutlet weak var repostBtn: UIButton!
    //点赞
    @IBOutlet weak var ohYeah: UIButton!
    //底部工具条
    @IBOutlet weak var toolBar: UIView!
    //被转发微博
    @IBOutlet weak var retweetedLabel: UILabel!
    
    
    
    //计算行高时使用
    func rowHeight(viewModel : HJStatusViewModel) ->CGFloat {
        
        self.viewModel = viewModel
        
        // 提前刷新
        self.contentView.layoutIfNeeded()
        
        return toolBar.frame.maxY
    }
    
    
    
    
    var viewModel : HJStatusViewModel? {
        didSet {
            iconView.sd_setImage(with: viewModel?.iconURL)
            timeLable.text = viewModel?.timeText
            
            
            nameLable.text = viewModel?.status?.user?.name
            sourceLable.text = viewModel?.sourceText
            contentLable.text = viewModel?.status?.text
            avaterImage.image = viewModel?.avatarImage
            memberImage.image = viewModel?.memberImage
            
            //获取配图视图的个数
            let count = viewModel?.pictureInfos?.count ?? 0
            
            let value = changePictureViewSize(count: count)
            //更新约束值
            pictureViewWidthCons.constant = value.pSize.width
            pictureViewHeightCons.constant = value.pSize.height
            //设置cell的大小
            pictureViewLayout.itemSize = value.itemSize
            
            //根据是否有配图调整顶部间距
            pictureViewTopCons.constant = (count == 0 ? 0 : commonMargin)
           
            //给配图视图设置数据源
           pictureView.pictureInfos = viewModel?.pictureInfos
           
            //给toolbar控件赋值
            repostBtn.setTitle(viewModel?.reposts_text, for: .normal)
            commentBtn.setTitle(viewModel?.comments_text, for: .normal)
            ohYeah.setTitle(viewModel?.attitudes_text, for: .normal)
            
            //被转发微博内容
            retweetedLabel?.text = viewModel?.status?.retweeted_status?.text
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        //
        selectionStyle = .none
       contentLable.preferredMaxLayoutWidth = KScreenW - 2 * commonMargin
       retweetedLabel?.preferredMaxLayoutWidth = KScreenW - 2 * commonMargin
        
        //设置配视图的flowLayout
        pictureViewLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        pictureViewLayout.minimumLineSpacing = pictureCellMargin
        pictureViewLayout.minimumInteritemSpacing =  pictureCellMargin
    }

    
    //根据图片布置配图视图每行的数量
    //itemSize:单张图片的大小  pSize总图的大小
    private func changePictureViewSize(count : Int) -> (pSize: CGSize , itemSize: CGSize){
        
        if count == 0 {
            return (CGSize.zero,CGSize.zero)
        }
        if count == 1 {
            
            //获取路径
            let urlString = viewModel?.pictureInfos?.first?.wap_pic ?? ""
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: urlString)
            
            let imageSize = image!.size
            
            return (imageSize , imageSize)
        }
        
        if count == 4 {
            let width = itemWidth * 2 + pictureCellMargin
            return (CGSize(width: width, height: width),CGSize(width: itemWidth ,height: itemWidth))
        }
        
        let rowCount = CGFloat((count - 1)/3 + 1)
        let height = (rowCount * itemWidth) + (rowCount - 1) * pictureCellMargin
         return (CGSize(width: maxWidth, height: height),CGSize(width: itemWidth, height: itemWidth))
 
    }
    
}












